@extends('layouts.app')
@section('content')
    <div class="content">
        <a class="btn btn-info btn-sm" href="{{ redirect()->back()->getTargetUrl() }}"> 返回 </a>
        <br />
        <br />
        {!! form($form) !!}
    </div>
@endsection
@push("stylesheets")
    <style>
        .select2-container--default .select2-selection--multiple .select2-selection__choice {
            color: gray;
        }
    </style>
@endpush

@push("scripts")
<script>
    $('.content').find('form').addClass('form-inline')
    $('.content').find('form').find('label').attr('style', 'width:120px;text-align:center')
    $('.form-group').attr('style', 'margin:10px 0px;')

    $('[for="shop_id"]').parent().after('<br>');
    $('[for="title"]').parent().after('<br>');
    $('[for="package_id[]"]').parent().after('<br>');
    $('[for="content"]').parent().after('<br>');
</script>
    <script>

        $("#shops-select.select2").select2({
            width: "500px",
            ajax: {
                dataType: 'json',
                delay: 250,
                url: function () {
                    return '{{ route('admin.gift.search_shops') }}';
                },
                processResults: function (data) {
                    return {
                        results: $.map(data, function(row, key) {
                            return {
                                id: key,
                                text: row
                            };
                        })
                    };
                }
            }
        });

        getShopType($('#shops-select').val())
        $('#shops-select').on('change', function () {
            shop_id = $(this).val();
            getShopType(shop_id, true)
        })

        // 查询门店类型
        function getShopType(shop_id = 0, is_change = false){
            getPackages(shop_id, is_change)
            // if (shop_id == null) {
                // $('#goods-select').parent('.form-group').hide();
                // $('#package-select').parent('.form-group').hide();
            // }else{
                $.ajax({
                    url:'{{ route('admin.get_shop_type') }}',
                    type: "GET",
                    dataType:'json',
                    data: {
                        shop_id: shop_id,
                    },
                    success: function (re) {
                        if (re.type == '婚纱摄影') {
                            getPackages(re.id, is_change)
                        }else{
                            // getGoods(re.id, is_change)
                        }
                    }
                });
            // }
        }

        function getPackages(shop_id = 0, is_change = false){
            $("#package-select.select2").select2({
                width: "650px",
                ajax: {
                    dataType: 'json',
                    delay: 250,
                    url: function () {
                        return '{{ route('admin.search_package') }}' + '?shop_id=' + shop_id;
                    },
                    processResults: function (data) {
                        return {
                            results: $.map(data, function(row, key) {
                                return {
                                    id: key,
                                    text: row
                                };
                            })
                        };
                    }
                }
            });
        }
        
    </script>
@endpush
